package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.common.entity.payconfig.EntityOrderEvaluate;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.EvaluateListRequest;
import com.ocom.common.utils.StringUtils;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoOrderEvaluate;
import com.ocom.payconfig.service.OrderEvaluateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class OrderEvaluateServiceImpl extends ServiceImpl<IDaoOrderEvaluate, EntityOrderEvaluate> implements OrderEvaluateService {


    @Autowired
    private IDaoOrderEvaluate iDaoOrderEvaluate;

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.SLAVE)
    public Map<String, Object> evaluateList(Long comId, EvaluateListRequest request) {
        QueryWrapper<EntityOrderEvaluate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", request.getComId());

        if (StringUtils.isNotEmpty(request.getOrderNo())) {
            queryWrapper.eq("order_no", request.getOrderNo());
        }
        if (request.getMerchId() != null && request.getMerchId() >= 0) {
            queryWrapper.eq("merch_id", request.getMerchId());
        }
        if (request.getPointId() != null && request.getPointId() >= 0) {
            queryWrapper.eq("point_id", request.getPointId());
        }
        PageHelper.startPage(request.getPage(), request.getSize());
        List<EntityOrderEvaluate> list = this.list(queryWrapper);
        PageInfo<EntityOrderEvaluate> pageInfo = new PageInfo<>(list);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("list", pageInfo.getList());
        return map;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, companyId = "comId", isMaster = MasterSlaveEnum.MASTER)
    public Boolean evaluateAdd(Long comId, EntityOrderEvaluate entityOrderEvaluate) {
        return this.save(entityOrderEvaluate);
    }
}
